package com.frank.big.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 小石潭记
 * @date 2020/10/21 11:03
 * @Description: ${todo}
 */
public class SHA1 {

    public static String encrypt(String str) {
        String result = "";
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte[] messageDigest = digest.digest();
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为十六进制数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            result = hexString.toString();
            return result;
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e.getMessage());
        }
        return result;
    }

}
